home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter7 / colattr.c < prev    next >
C/C++ Source or Header  |  1996-04-28  |  9KB  |  277 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "colattr.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLColAttributes()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static HENV  hEnv  = NULL;
  111. static HDBC  hDBC  = NULL;
  112. static HWND  hList = NULL;
  113. static UCHAR szLBStr[256];
  114. UCHAR  szSqlStr[]  = "Select dept_id, dept_name, dept_head_id From department";
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE :
  119.               {
  120.                  RETCODE retCode;
  121.  
  122.                  // Allocate Environment and Connection.
  123.                  //.....................................
  124.                  SQLAllocEnv( &hEnv );
  125.                  SQLAllocConnect( hEnv, &hDBC );
  126.  
  127.                  // Connect to the data source
  128.                  //...........................
  129.                  retCode = SQLConnect( hDBC, 
  130.                              "Test Data Source", SQL_NTS,
  131.                              "DBA",              SQL_NTS,  
  132.                              "SQL",              SQL_NTS );
  133.  
  134.                  if ( retCode != SQL_SUCCESS )
  135.                  {
  136.                     SQLFreeConnect( hDBC );
  137.                     SQLFreeEnv( hEnv );
  138.                     return( -1 );
  139.                  }
  140.  
  141.                  if ( retCode == SQL_SUCCESS )
  142.  
  143.                  hList = CreateWindow( "LISTBOX", "",    
  144.                                        LBS_NOTIFY | WS_VSCROLL | 
  145.                                        WS_BORDER  | WS_CHILD | 
  146.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  147.                                        0, 0, 
  148.                                        0, 0,  
  149.                                        hWnd,              
  150.                                        (HMENU)101,              
  151.                                        hInst,         
  152.                                        NULL               
  153.                                      );
  154.               }
  155.               break;
  156.  
  157.       case WM_SIZE :
  158.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  159.                                        HIWORD( lParam ), TRUE );
  160.               break; 
  161.               
  162.       case WM_COMMAND :
  163.               switch( LOWORD( wParam ) )
  164.               {
  165.                  case IDM_TEST :
  166.                         {
  167.                            HSTMT   hStmt;
  168.                            RETCODE retCode;
  169.   
  170.                            // Allocate a statement handle for the query.
  171.                            // ..........................................
  172.                            SQLAllocStmt( hDBC, &hStmt );
  173.  
  174.                            // Call SQLExecDirect() to execute the query.
  175.                            // ..........................................
  176.                            retCode = SQLExecDirect( hStmt, szSqlStr, SQL_NTS );
  177.                            
  178.                            if( retCode == SQL_SUCCESS ||
  179.                                retCode == SQL_SUCCESS_WITH_INFO )
  180.                            {
  181.                               SDWORD fDesc;
  182.  
  183.                               // Call SQLColAttributes() to determine 
  184.                               // whether or not the dept_id column is
  185.                               // updatable.
  186.                               // ....................................
  187.  
  188.                               retCode = SQLColAttributes( hStmt, 1, 
  189.                                            SQL_COLUMN_UPDATABLE, NULL, 
  190.                                            0, NULL, &fDesc );
  191.  
  192.                               if( retCode != SQL_SUCCESS &&
  193.                                   retCode != SQL_SUCCESS_WITH_INFO )
  194.                                  break;
  195.  
  196.                               strcpy( szLBStr, "Attribute is " );
  197.  
  198.                               switch( fDesc )
  199.                               {
  200.                                  case SQL_ATTR_READONLY:
  201.                                     strcat( szLBStr, "SQL_ATTR_READONLY" );
  202.                                     break;
  203.  
  204.                                  case SQL_ATTR_WRITE:
  205.                                     strcat( szLBStr, "SQL_ATTR_WRITE" );
  206.                                     break;
  207.  
  208.                                  case SQL_ATTR_READWRITE_UNKNOWN:
  209.                                     strcat( szLBStr, 
  210.                                             "SQL_ATTR_READWRITE_UNKNOWN" );
  211.                                     break;
  212.                               }
  213.                               
  214.                               SendMessage( hList, LB_ADDSTRING, 0, 
  215.                                                   (LPARAM)szLBStr );
  216.                            }
  217.                            
  218.                            // Free the statement handle.
  219.                            // ..........................
  220.                            SQLFreeStmt( hStmt, SQL_DROP );
  221.                         }
  222.                         break;
  223.  
  224.                  case IDM_ABOUT :
  225.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  226.                         break;
  227.  
  228.                  case IDM_EXIT :
  229.                         DestroyWindow( hWnd );
  230.                         break;
  231.               }
  232.               break;
  233.       
  234.       case WM_DESTROY :
  235.               PostQuitMessage(0);
  236.  
  237.               // Disconnect Environment.
  238.               //........................
  239.               SQLDisconnect( hDBC );
  240.  
  241.               // Free Connection and Environment.
  242.               //.................................
  243.               SQLFreeConnect( hDBC );
  244.               SQLFreeEnv( hEnv );
  245.               break;
  246.  
  247.       default :
  248.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  249.    }
  250.  
  251.    return( 0L );
  252. }
  253.  
  254.  
  255. LRESULT CALLBACK About( HWND hDlg,           
  256.                         UINT message,        
  257.                         WPARAM wParam,       
  258.                         LPARAM lParam)
  259. {
  260.    switch (message) 
  261.    {
  262.        case WM_INITDIALOG: 
  263.                return (TRUE);
  264.  
  265.        case WM_COMMAND:                              
  266.                if (   LOWORD(wParam) == IDOK         
  267.                    || LOWORD(wParam) == IDCANCEL)    
  268.                {
  269.                        EndDialog(hDlg, TRUE);        
  270.                        return (TRUE);
  271.                }
  272.                break;
  273.    }
  274.  
  275.    return (FALSE); 
  276. }
  277.